using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace TelemedycynaLab
{
    public partial class Form1 : Form
    {
        const string PACJENT = "Jan Kowalski";
        const string URL = "http://www.ciel.pl/studenci/tm-lab/lab.html";

        //dane w formacie czas, wartosc
        string[,] daneTab = new string[10,2];
        int daneTabWskaznik = 0;
               
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = "COM18";
        }

        private void PiszWartoscAktualna(string msg)
        {
            label2.Invoke(new EventHandler(delegate { label2.Text = msg; }));

        }

        private void PiszOdpowiedz(string msg)
        {
            label5.Invoke(new EventHandler(delegate { label5.Text = msg; }));   
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            //pobieram linie z portu i usuwam znak nowej linii
            string dane = serialPort1.ReadLine().Replace("\r", String.Empty);
          
            //wypisuje aktualna wartosc
            PiszWartoscAktualna(dane);

     
            
            //odczytana wartosc wpisuje do tablicy
            if (daneTabWskaznik < 10)
            {
                daneTab[daneTabWskaznik, 0] = DateTime.Now.ToString();
                daneTab[daneTabWskaznik, 1] = dane;
                daneTabWskaznik++;

            }
            else
            {
                //tworze z tablicy link  w postaci:
                //name=ImieNazwisko&date1=2011&value1=1&date2=2022&value2=10
                StringBuilder sb = new StringBuilder();
                sb.Append("name=").Append(PACJENT);
                for (int i = 0; i < 10; i++)
                {
                    sb.Append("&date").Append(i).Append("=").Append(daneTab[i, 0]);
                    sb.Append("$value").Append("=").Append(daneTab[i, 1]);

                }

                //wysylam przez POST
                string odpowiedz = HttpPost(URL, sb.ToString());
                PiszOdpowiedz(odpowiedz);

                //pisze odpowiedz
                
                sb.Length = 0;
                daneTabWskaznik = 0;
                
            }
        }

        //post
        private string HttpPost(string uri, string parameters)
        {
            // parameters: name1=value1&name2=value2	
            WebRequest webRequest = WebRequest.Create(uri);
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method = "POST";
            byte[] bytes = Encoding.ASCII.GetBytes(parameters);
            Stream os = null;
            try
            { // send the Post
                webRequest.ContentLength = bytes.Length;   //Count bytes to send
                os = webRequest.GetRequestStream();
                os.Write(bytes, 0, bytes.Length);         //Send it
            }
            catch (WebException ex)
            {
                MessageBox.Show(ex.Message, "HttpPost: Request error",
                   MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (os != null)
                {
                    os.Close();
                }
            }

            try
            { // get the response
                WebResponse webResponse = webRequest.GetResponse();
                if (webResponse == null)
                { return null; }
                StreamReader sr = new StreamReader(webResponse.GetResponseStream());
                return sr.ReadToEnd().Trim();
            }
            catch (WebException ex)
            {
                MessageBox.Show(ex.Message, "HttpPost: Response error",
                   MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return null;
        }

        //obsluga przycisku start 
        private void button1_Click(object sender, EventArgs e)
        {
            
        }
    }
}
 //$URL:  $
 //$Id: $